import Vue from 'vue'
import App from './App.vue'
import router from './router'
import './assets/common.css'
import ElementUI from 'element-ui'
import axios from 'axios'
import NProgress from 'nprogress'
// 引入加载条样式
import 'nprogress/nprogress.css'

// 引入公共面包屑组件
import BreadCrumb from './components/BreadCrumb.vue'
axios.defaults.baseURL = 'http://127.0.0.1:11333/api/private/v1/'
Vue.use(ElementUI)
// 全局组件
Vue.component('bread-crumb', BreadCrumb)

Vue.prototype.$http = axios
Vue.config.productionTip = false

axios.interceptors.request.use(
  function (config) {
    NProgress.start()
    const token = sessionStorage.getItem('token')
    // console.log(config)
    // 给axios配置token，传递给服务器
    if (token !== null) {
      config.headers.Authorization = token
    }
    return config
  },
  function (error) {
    return Promise.reject(error)
  }
)

new Vue({
  created () {
    this.$http.interceptors.response.use(
      function (response) {
        NProgress.done()
        // 对响应数据做点什么
        if (response.status !== 200 && response.status !== 201) {
          return this.$message.error(response.data.meta.msg)
        }
        return response
      },
      function (error) {
        // 对响应错误做点什么
        return Promise.reject(error)
      }
    )
  },
  router,
  render: h => h(App)
}).$mount('#app')
